package com.leoman.reourceapi.service.impl;

import com.leoman.entity.Constant;
import com.leoman.exception.*;
import com.leoman.indexapi.service.UserTaskApiService;
import com.leoman.post.entity.vo.PostImageVo;
import com.leoman.reourceapi.service.ResourceApiService;
import com.leoman.resources.dao.GameResourcesDao;
import com.leoman.resources.dao.MusicResourceDao;
import com.leoman.resources.dao.StillResourceDao;
import com.leoman.resources.entity.GameResource;
import com.leoman.resources.entity.MusicResource;
import com.leoman.resources.entity.StillResource;
import com.leoman.resources.entity.vo.MusicVo;
import com.leoman.user.dao.UserInfoDao;
import com.leoman.user.entity.UserInfo;
import com.leoman.userapi.service.impl.UserInfoServiceImpl;
import com.leoman.utils.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 资源接口实现类
 * Created by 涂奕恒 on 2016/6/12.
 */
@Service
public class ResourceApiServiceImpl implements ResourceApiService {

    @Autowired
    private StillResourceDao stillResourceDao;

    @Autowired
    private MusicResourceDao musicResourceDao;

    @Autowired
    private GameResourcesDao gameResourcesDao;

    @Autowired
    private UserTaskApiService userTaskApiService;

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public Page<StillResource> iPageStillList(Integer pageNum, Integer pageSize) {
        int[] info = changePageParams(pageNum, pageSize);
        Page<StillResource> page = stillResourceDao.iPageListGroupByCategoryId(new PageRequest(info[0] - 1, info[1]));

        for (StillResource stillResource : page.getContent()) {
            stillResource.setName(stillResource.getCategory().getName());
            stillResource.setImageUrl(ImageUtil.clearUploadUrl(stillResource.getSuolue()));
            stillResource.setSuolue(ImageUtil.clearUploadUrl(stillResource.getSuolue()));
        }

        return page;
    }

    @Override
    public StillResource iFindOneStill(Long id) throws ParamsErrorException, WelfareNotFindException {
        if (null == id) {
            throw new ParamsErrorException();
        }
        StillResource stillResource = stillResourceDao.findOne(id);
        if (null == stillResource) {
            throw new WelfareNotFindException();
        }
        stillResource.setName(stillResource.getCategory().getName());
        stillResource.setImageList(getStillVo(stillResource.getCategory().getId()));
        stillResource.setImageUrl(ImageUtil.clearUploadUrl(stillResource.getSuolue()));
        stillResource.setSuolue(ImageUtil.clearUploadUrl(stillResource.getSuolue()));

        return stillResource;
    }

    @Override
    public Page<MusicResource> iPageMusicList(Integer pageNum, Integer pageSize) {
        int[] info = changePageParams(pageNum, pageSize);
        Page<MusicResource> page = musicResourceDao.iPageListGroupByCategoryId(new PageRequest(info[0] - 1, info[1]));

        for (MusicResource musicResource : page.getContent()) {
            musicResource.setName(musicResource.getCategory().getName());
            musicResource.setCover(ImageUtil.clearUploadUrl(musicResource.getCategory().getSuolue()));
            musicResource.setMusicList(getMusicVo(musicResource.getCategory().getId()));
            musicResource.setSize(musicResource.getMusicList().size());
        }

        return page;
    }

    @Override
    public MusicResource iFindOneMusic(Long id) throws ParamsErrorException, WelfareNotFindException {
        if (null == id) {
            throw new ParamsErrorException();
        }
        MusicResource musicResource = musicResourceDao.findOne(id);
        if (null == musicResource) {
            throw new WelfareNotFindException();
        }
        musicResource.setName(musicResource.getCategory().getName());
        musicResource.setCover(ImageUtil.clearUploadUrl(musicResource.getCategory().getUrl()));
        musicResource.setMusicList(getMusicVo(musicResource.getCategory().getId()));
        musicResource.setSize(musicResource.getMusicList().size());

        return musicResource;
    }

    @Override
    public Page<GameResource> iPageGameList(Integer pageNum, Integer pageSize) {
        int[] info = changePageParams(pageNum, pageSize);
        Page<GameResource> page = gameResourcesDao.findAll(new PageRequest(info[0] - 1, info[1]));

        for (GameResource gameResource : page.getContent()) {
            gameResource.setUrl(ImageUtil.clearUploadUrl(gameResource.getUrl()));
            gameResource.setSuolue(ImageUtil.clearUploadUrl(gameResource.getSuolue()));
        }

        return page;
    }

    @Override
    public GameResource iFindOneGame(Long id, Long userId)
            throws ParamsErrorException,
            WelfareNotFindException,
            UserNotFindException,
            UserStatusErrorException {
        if (null == id) {
            throw new ParamsErrorException();
        }

        GameResource gameResource = gameResourcesDao.findOne(id);
        if (null == gameResource) {
            throw new WelfareNotFindException();
        }
        gameResource.setUrl(ImageUtil.clearUploadUrl(gameResource.getUrl()));
        gameResource.setSuolue(ImageUtil.clearUploadUrl(gameResource.getSuolue()));

        if (null != userId) {
            UserInfo userInfo = UserInfoServiceImpl.getUserInfo(userInfoDao, userId);
            userTaskApiService.addResourceDetailData(id, 3, userInfo, "进入资源");
        }

        return gameResource;
    }

    @Override
    @Transactional
    public Boolean downResource(Long id, Integer type, Long userId, Integer style)
            throws ParamsErrorException,
            ResourceNotFindException,
            UserNotFindException,
            UserStatusErrorException {
        if (null == id || null == type) {
            throw new ParamsErrorException();
        }

        Boolean flag = false;

        GameResource gameResource = null;
        String resourceName = "";

        switch (type) {
            case 1:
                // 剧照
                StillResource stillResource = stillResourceDao.findOne(id);
                if (null != stillResource) {
                    resourceName = stillResource.getName();
                    stillResource.setNum(stillResource.getNum() + 1);

                    stillResourceDao.save(stillResource);
                    flag = true;
                }
                break;
            case 2:
                // 音乐
                MusicResource musicResource = musicResourceDao.findOne(id);
                if (null != musicResource) {
                    resourceName = musicResource.getName();
                    musicResource.setNum(musicResource.getNum() + 1);
                    System.out.println();
                    musicResourceDao.save(musicResource);
                    flag = true;
                }
                break;
            case 3:
                // 游戏
                gameResource = gameResourcesDao.findOne(id);
                if (null != gameResource) {
                    resourceName = gameResource.getName();
                    gameResource.setDownCount(gameResource.getDownCount() + 1);

                    gameResourcesDao.save(gameResource);
                    flag = true;
                }
                break;
        }

        // 统计资源数据
        if (null == style || style == 1) {
            userTaskApiService.addResourceData(id, resourceName, type, 1, 0);
        } else {
            userTaskApiService.addResourceData(id, resourceName, type, 0, 1);
        }
        if (null != gameResource && null != userId) {
            UserInfo userInfo = UserInfoServiceImpl.getUserInfo(userInfoDao, userId);
            userTaskApiService.addResourceDetailData(id, type, userInfo, "点击下载——" + gameResource.getName());
        }

        return flag;
    }

    /**
     * 根据类别id获取音乐列表
     */
    private List<MusicVo> getMusicVo(Long categoryId) {
        List<MusicVo> list = new ArrayList<>();
        MusicVo musicVo;
        List<MusicResource> musicResourceList = musicResourceDao.iFindListByCategoryId(categoryId);

        for (MusicResource musicResource : musicResourceList) {
            musicVo = new MusicVo();
            musicVo.setId(musicResource.getId());
            musicVo.setName(musicResource.getName());
            musicVo.setSingerName(musicResource.getSinger());
            musicVo.setPlayNum(musicResource.getNum());
            musicVo.setUrl(ImageUtil.clearUploadUrl(musicResource.getImageUrl()));

            list.add(musicVo);
        }

        return list;
    }

    /**
     * 根据类别id获取剧照列表
     */
    private List<PostImageVo> getStillVo(Long categoryId) {
        List<PostImageVo> list = new ArrayList<>();

        PostImageVo image;
        List<StillResource> stillResourceList = stillResourceDao.iFindListByCategoryId(categoryId);

        for (StillResource still : stillResourceList) {
            image = new PostImageVo();
            image.setId(still.getId());
            image.setUrl(ImageUtil.clearUploadUrl(still.getImageUrl()));
            image.setSuolue(ImageUtil.clearUploadUrl(still.getSuolue()));
            image.setWidth(still.getWidth());
            image.setHeight(still.getHeight());

            list.add(image);
        }

        return list;
    }

    public static int[] changePageParams(Integer pageNum, Integer pageSize) {
        int[] info = new int[2];

        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = Constant.PAGE_DEF_SIZE;
        }

        info[0] = pageNum;
        info[1] = pageSize;

        return info;
    }
}
